package com.xml;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;

public class AbstractRecordExtract {
	protected List<Record> getRecordList(TableTag aTableTag) {

		List<Record> recordList = new ArrayList<Record>();
		int rowCount = aTableTag.getRowCount();
		int colCount = 0;
		TableRow tr = null;
		String[] tdc = null;
		// 第一列是列名
		TableColumn[] tds = null;
		if (rowCount > 0) {
			tr = aTableTag.getRow(0);
			colCount = tr.getColumnCount();
			if (colCount > 0) {
				tds = tr.getColumns();
				tdc = new String[colCount];
				for (int i = 0; i < colCount; i++) {
					tdc[i] = tds[i].toPlainTextString();
				}
			}

		}

		// 以后都是数据
		if (rowCount > 1) {
			Record aRecord = null;
			for (int i = 1; i < rowCount; i++) {
				tr = aTableTag.getRow(i);
				TableColumn[] fvs = tr.getColumns();
				aRecord = new Record();
				aRecord.setTableName(aTableTag.getAttribute("name"));

				String operation = aTableTag.getAttribute("operation");
				aRecord.op = operation;

				for (int j = 0; j < colCount; j++) {
					String fieldName = tdc[j];
					String value = fvs[j].toPlainTextString();
					aRecord.put(fieldName.toUpperCase(), value);
				}
				recordList.add(aRecord);
			}

		}
		return recordList;
	}

}
